package com.zili.sink;

import com.zili.source.Event;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.util.serialization.KeyedSerializationSchema;

import java.util.Properties;

/**
 * @author : ranzlupup
 * @since : 2023/3/8 15:14
 */
public class SinkToKafka2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 1.从kafka中读取数据
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "hadoop102:9092");
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");

        DataStreamSource<String> kafkaStream = env.addSource(new FlinkKafkaConsumer<String>(
                "atcper",
                new SimpleStringSchema(),
                properties
        ));

        // 2.用flink进行转换处理
        SingleOutputStreamOperator<String> mapStreamOperator = kafkaStream.map(
                new MapFunction<String, String>() {
                    @Override
                    public String map(String value) throws Exception {
                        String[] fields = value.split(",");
                        return "user-" + fields[0] + "-url-" + fields[1].trim() + "-time-" + fields[2].trim();
                    }
                }
        );

        // 3.结果数据写入kafka
        mapStreamOperator.addSink(new FlinkKafkaProducer<String>(
                "hadoop102:9092",
                "events",
                new SimpleStringSchema()
        ));

        env.execute();
    }
}
